package com.fhzn.auth.web.converter;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.fhzn.auth.constant.ResourceTypeEnum;
import com.fhzn.auth.entity.Resource;
import com.fhzn.auth.tools.DateTimeUtils;
import com.fhzn.auth.web.vo.ResourceVO;
import com.fhzn.commons.toolkit.exception.BuzException;
import com.google.common.collect.Lists;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author 老顾
 * @apiNote
 * @since 2023/12/20
 */
public interface ResourceConverter {

    /**
     * 资源实体转换
     *
     * @param resource 请求参数
     * @return 返回参数
     */
    static ResourceVO fromResource(Resource resource) {
        ResourceVO vo = new ResourceVO();
        if (resource != null) {
            vo.setStatus(resource.getStatus());
            vo.setId(resource.getId());
            vo.setAppCode(resource.getAppCode());
            vo.setCode(resource.getCode());
            vo.setSort(resource.getSort());
            vo.setComment(resource.getComment());
            vo.setName(resource.getName());
            vo.setLevel(resource.getLevel());
            vo.setParentCode(resource.getParentCode());
            vo.setResourceType(resource.getResourceType());
            vo.setResourceUrl(resource.getResourceUrl());
            vo.setModifier(resource.getModifier());
            if (resource.getUpdatedTime() != null) {
                vo.setUpdatedTime(DateTimeUtils.format(resource.getUpdatedTime()));
            }
        }
        return vo;
    }

    /**
     * 资源实体转换
     *
     * @param resources 请求参数
     * @return 返回参数
     */
    static List<ResourceVO> convertToVoList(List<Resource> resources) {
        if (CollectionUtils.isEmpty(resources)) {
            return Lists.newArrayList();
        }
        return resources.stream().map(ResourceConverter::fromResource).collect(Collectors.toList());
    }
}
